<?php

class Likes 
{
	 	 
	 public static function add_video_like($p_videos_link_name, $p_author_ip, $p_like_status)
	{
		 $count_of_like=SQL::num_rows(Template::render("CheckVideoLikeStatus.sql", "sql", array
		 (
			 'author_ip'=>$p_author_ip,
			 'videos_link_name'=>$p_videos_link_name
		 )));	
		 
		 if ($count_of_like == 0)
		{
			 SQL::execute(Template::render("addVideoLike.sql", "sql", array
			 (
				 'author_ip'=>$p_author_ip,
				 'videos_link_name'=>$p_videos_link_name,
				 'status'=>$p_like_status
			 )));	
		}			 
		 else 
		{
			 $data=SQL::select(Template::render("CheckVideoLikeStatus.sql", "sql", array
			 (
				 'author_ip'=>$p_author_ip,
				 'videos_link_name'=>$p_videos_link_name
			 )));	
			
			$old_like_status=$data[0]["status"];
			
			if ($old_like_status!=$p_like_status)
			{
				 SQL::execute(Template::render("updateVideoLikeStatus.sql", "sql", array
				 (
					 'author_ip'=>$p_author_ip,
					 'videos_link_name'=>$p_videos_link_name,
					 'status'=>$p_like_status
				 )));	
			}				 
		}
		
		 return self::getVideoLikesCount($p_videos_link_name);	
		
	}
	
	 public static function add_photo_like($p_photos_link_name, $p_author_ip, $p_like_status)
	{
		 $count_of_like=SQL::num_rows(Template::render("CheckPhotoLikeStatus.sql", "sql", array
		 (
			 'author_ip'=>$p_author_ip,
			 'photos_link_name'=>$p_photos_link_name
		 )));	
		 
		 if ($count_of_like == 0)
		{
			 SQL::execute(Template::render("addPhotoLike.sql", "sql", array
			 (
				 'author_ip'=>$p_author_ip,
				 'photos_link_name'=>$p_photos_link_name,
				 'status'=>$p_like_status
			 )));	
		}			 
		 else 
		{
			 $data=SQL::select(Template::render("CheckPhotoLikeStatus.sql", "sql", array
			 (
				 'author_ip'=>$p_author_ip,
				 'photos_link_name'=>$p_photos_link_name
			 )));	
			
			$old_like_status=$data[0]["status"];
			
			if ($old_like_status!=$p_like_status)
			{
				 SQL::execute(Template::render("updatePhotoLikeStatus.sql", "sql", array
				 (
					 'author_ip'=>$p_author_ip,
					 'photos_link_name'=>$p_photos_link_name,
					 'status'=>$p_like_status
				 )));	
			}				 
		}
		
		 return self::getPhotoLikesCount($p_photos_link_name);	
		
	}
	
	public static function getVideoLikesCount ($p_videos_link_name)
	{
		 return $data=SQL::select(Template::render("getVideoLikesCount.sql", "sql", array
		(
			 'videos_link_name'=>$p_videos_link_name
		 )));	
	}
	
	public static function getPhotoLikesCount ($p_photos_link_name)
	{
		 return $data=SQL::select(Template::render("getPhotoLikesCount.sql", "sql", array
		(
			 'photos_link_name'=>$p_photos_link_name
		 )));	
	}
	

}
